<?php
class MemberAction extends Action {
    public function login(){
        init($this);
        $this->MenuSelected     = 'Login';
        $data['LoginName'] = $this->_post('loginname');
        $data['PassWord'] = md5($this->_post('password'));
        $this->error = "&nbsp";
        if (isset($data['LoginName']) && isset($data['PassWord'])) {
            $user = M('User');
            $res = $user->where($data)->select();
            if (isset($res)) {
                foreach ($res[0] as $key => $value) {
                    $_SESSION[$key] = $value;
                }
                // sign in success
                echo "<script>window.location.href='".$this->UrlList["Home"]."';</script>";
            }
            else {
                $this->error = "错误的用户名或密码";
            }
        }
        $this->display();
    }
    public function logout() {
        init($this);
        session_destroy();
        echo "<script>window.location.href='".$this->UrlList["Index"]."';</script>";
    }
    public function adduser(){
        init($this);
        if ($this->UserLevel != 3) {
            gohistory();
        }
        else {
            $this->MenuSelected = 'AddUser';
            $data['LoginName'] = $this->_post('loginname');
            $data['PassWord'] = md5($this->_post('password'));
            $data['UserName'] = $this->_post('username');
            $data['Email'] = $this->_post('email');
            $data['UserLevel'] = $this->_post('level');
            $this->error = "&nbsp";
            if (isset($data['LoginName']) && isset($data['PassWord'])) {
                // addition use Model for confim and md5 and so on
                $user = D('User');
                $user->create($data);
                $user->add();
                $this->error = $user->getError();
                if (!$this->error) {
                    $this->error = "添加成功";
                }
            }
            $this->display();
        }
    }
    public function usermanage() {
        init($this);
        if ($this->UserLevel != 3) {
            gohistory();
        }
        else {
            $this->MenuSelected = 'UserManage';
            $sql = "";
            $kind = $this->_get("kind");
            if (isset($kind))
                $sql = $sql."and user.UserLevel=kind".$kind;
            $model = new Model();
            $res = $model->table("user")
                         ->join("`select` on select.UserId=user.UserId")
                         ->field("user.UserId, user.UserLevel, user.UserName, user.Email, count(SubId)")
                         ->where("user.UserLevel!=3".$sql)
                         ->group("user.UserId")
                         ->order("user.UserId")
                         ->select();
            $this->UserList = $res;
            $this->display();
        }
    }
    public function del() {
        init($this);
        if ($this->UserLevel == 3) {
            // need to add a valid property
            $userid = $this->_get("uid");
            $sql = M('User');
            $sql->delete($userid);
        }
        gohistory();
    }
}
?>
